<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>has $.Examples14 handles 'Perl6';</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="050601.html">module updated</a>&nbsp;&nbsp;>>Next: <a href="050603_ubuntu.html">Thank God, ubuntu rocks</a></p>
<h1>has $.Examples14 handles 'Perl6';</h1>
<div class='content'>
<p>Category: <a href='Perl6.html'>Perl6</a> &nbsp; Keywords: <b>delegation handles Perl6</b></p><h3>Delegation</h3>
Delegation, 中文的意思可以为代表，授权或是委托。<br />
Delegation 能让你在一个 class 或 role 里把另一个对象的方法当成自己的。<br />

<h3>例子用于理解</h3>
<pre>class Dog {
    method wag { 2 }
    method run { 100 }
}
class Wolf {
    method wag { 4 }
}
class Snoopy {
    has $.tail handles 'wag';
}
my $pet = Snoopy.new;
# $pet.wag; 错误，不知道 $.tail 对应哪个类
$pet.tail = Dog.new; # 这里的 $pet 没有继承 Dog 类
$pet.wag; # 2
$pet.tail = Wolf.new; # 将 $.tail 对应到 Wolf
$pet.wag; # 4, $.tail 对应 Wolf, 所以调用 Wolf.wag;</pre>
<p>其中最关键的一句就是 has $.tail handles 'wag'; 它使类 Snoopy 产生了一个方法 wag. 而调用 wag 就等同于调用 $.tail.wag 所以这话也就等同于在类里定义了如下代码：
<pre>method wag (*@args is context(Lazy)) { $.tail.wag(*@args) }</pre>
关键字 handles 的中文意思可以为处理。所以代码 has $.tail handles 'wag' 的字面意思理解可以为“用 $.tail 来处理 wag 调用”。

<h3>方法多样化</h3>
<ul>
<li>如果要多于一个方法的话，可以这么表示：
<pre><code>has $.tail handles &lt;wag run>;</code></pre>
<li>其实我们是用智能匹配来处理 handles 后面的参数。比如：
<pre>has $:fur handles /^get_/;
# 智能匹配正则表达式，所以以 get_ 开头的方法调用都委托 $:fur 对象来处理

has $:fur handles Groomable;
# handles 的方法就是 Groomable 里的所有方法</pre>
<li>如果你想定义一个方法名字而实际调用的是另一个方法，可以使用 Pair.
<pre><code>has $:fur handles { :shakefur&lt;shake> :scratch&lt;get_fleas> };
# 当你调用 shakefur 时实际上调用了 $:fur.shake</code></pre>
<li>可以更进一步，使用正则替换来改变方法名：
<pre>has $:fur handles (s/^furget_/get_/);
# 当你调用 furget_a 时实际调用的是 get_a</pre>
</ul>

<h3>Want more?</h3>
See Synopsis12.</div>
<p><&lt;Previous: <a href="050601.html">module updated</a>&nbsp;&nbsp;>>Next: <a href="050603_ubuntu.html">Thank God, ubuntu rocks</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=has%20$.Examples14%20handles%20'Perl6';&url=http://www.fayland.org/journal/Perl6_ES14.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='050519.html'>Synopsis localization</a> < <span class='digit'>2005-05-19 11:21:25</span> ></li><li><a href='Perl6_ES0.html'>@Examples[0] is Perl6</a> < <span class='digit'>2005-05-19 20:26:08</span> ></li><li><a href='Perl6_ES1.html'>given @Examples[1] when Perl6</a> < <span class='digit'>2005-05-19 22:05:56</span> ></li><li><a href='Perl6_ES2.html'>@Perl6 <== @Examples xx 2</a> < <span class='digit'>2005-05-20 18:53:54</span> ></li><li><a href='Perl6_ES3.html'>my Perl6 @Examples[3]</a> < <span class='digit'>2005-05-21 13:53:16</span> ></li><li><a href='Perl6_ES4.html'>say q:2 '@*Examples.[4] &Perl6()';</a> < <span class='digit'>2005-05-22 12:24:12</span> ></li><li><a href='Perl6_ES5.html'>sub Perl6 (*@Examples[5] is copy)</a> < <span class='digit'>2005-05-22 15:37:26</span> ></li><li><a href='Perl6_ES6.html'>multi sub Examples (6, Perl6) {...}</a> < <span class='digit'>2005-05-23 22:43:33</span> ></li><li><a href='Perl6_ES7.html'>sub infix:<(7)> ($Perl6, @Examples)</a> < <span class='digit'>2005-05-24 02:16:42</span> ></li><li><a href='Perl6_ES8.html'>let @Examples[8] = 'Perl6';</a> < <span class='digit'>2005-05-24 13:05:58</span> ></li><li><a href='Perl6_ES9.html'>Perl6 | Examples & 9 ^ junction</a> < <span class='digit'>2005-05-26 12:35:29</span> ></li><li><a href='Perl6_ES10.html'>%Examples{'IO'} = open('Perl6');</a> < <span class='digit'>2005-05-30 00:24:01</span> ></li><li><a href='Perl6_ES11.html'>class Examples11 is Perl6 { ... }</a> < <span class='digit'>2005-05-30 13:31:31</span> ></li><li><a href='Perl6_ES12.html'>$Perl6 = $class.bless(:Example<12>);</a> < <span class='digit'>2005-05-31 11:38:46</span> ></li><li><a href='Perl6_ES13.html'>role Example13 { # Perl6 }</a> < <span class='digit'>2005-05-31 18:22:03</span> ></li><li><a href='Perl6_ES16.html'>my subtype Examples16 of Perl6 where /^kissu$/</a> < <span class='digit'>2005-06-04 23:59:29</span> ></li><li><a href='Perl6_ES17.html'>my enum Example17 <<:Perl(6) Today>></a> < <span class='digit'>2005-06-05 12:14:08</span> ></li><li><a href='Perl6_ES18.html'>my $queen; rule me {Perl6|Examples18}</a> < <span class='digit'>2005-06-06 12:26:32</span> ></li><li><a href='Perl6_ES19.html'>m:nth(19)/Perl6Examples/</a> < <span class='digit'>2005-06-06 22:02:01</span> ></li><li><a href='Perl6_Rules.html'>用 Perl6::Rules 来写 Formatter</a> < <span class='digit'>2005-09-27 10:02:45</span> ></li></ul>
Created on <span class="digit">2005-06-02 01:38:59</span>, Last modified on <span class="digit">2005-06-02 01:50:07</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>